package com.deliinsns.sys.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Convenience class for setting and retrieving cookies.
 */
public class CookieUtils {

	/**
	 * Convenience method to set a cookie
	 *
	 * @param response
	 * @param name
	 * @param value
	 * @param path
	 */
	public static void setCookie(HttpServletResponse response, String name,
								 String value, String path, String age) {

		Cookie cookie = new Cookie(name, value);
		cookie.setSecure(false);
		cookie.setPath(path);
		if (age != null) {
			int n = 2;
			try {
				n = Integer.parseInt(age);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			cookie.setMaxAge(3600 * n); // 2 hours
		}
		response.addCookie(cookie);
	}

	public static void setCookie(HttpServletResponse response, String name,
								 String value, String path) {
		setCookie(response, name, value, path, null);
	}

	public static void setCookie(HttpServletResponse response, String name,
								 String value) {
		setCookie(response, name, value, "/", null);
	}

	public static void setCookie(HttpServletResponse response, String name,
								 String value, String path, String age, String doMain) {

		Cookie cookie = new Cookie(name, value);
		cookie.setSecure(false);
		cookie.setPath(path);
		cookie.setDomain(doMain);
		if (age != null) {
			int n = 2;
			try {
				n = Integer.parseInt(age);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			cookie.setMaxAge(3600 * n); // 2 hours
		}

		response.addCookie(cookie);
	}

	/**
	 * Convenience method to get a cookie by name
	 *
	 * @param request the current request
	 * @param name    the name of the cookie to find
	 * @return the cookie (if found), null if not found
	 */
	public static Cookie getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		Cookie returnCookie = null;

		if (cookies == null) {
			return returnCookie;
		}

		for (Cookie thisCookie : cookies) {
			if (thisCookie.getName().equals(name)) {
				returnCookie = thisCookie;
				break;
			}
		}

		return returnCookie;
	}

	public static String getCookieValue(HttpServletRequest request, String name) {
		Cookie cookie = getCookie(request, name);
		if (cookie == null) {
			return null;
		}

		return cookie.getValue();
	}

	public static String getAllCookieNameAndValue(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		StringBuilder cs = new StringBuilder();

		for (Cookie cooky : cookies) {
			cs.append(cooky.getName()).append("=")
					.append(cooky.getValue()).append(";");
		}
		return cs.toString();
	}

	/**
	 * Convenience method for deleting a cookie by name
	 *
	 * @param response the current web response
	 * @param cookie   the cookie to delete
	 * @param path     the path on which the cookie was set (i.e. /appfuse)
	 */
	public static void deleteCookie(HttpServletResponse response,
									Cookie cookie, String path) {
		if (cookie != null) {
			// Delete the cookie by setting its maximum age to zero
			cookie.setMaxAge(0);
			cookie.setPath(path);
			response.addCookie(cookie);
		}
	}

	public static void deleteCookie(HttpServletResponse response,
									Cookie cookie) {
		deleteCookie(response, cookie, "/");
	}


	/**
	 * Convenience method to get the application's URL based on request
	 * variables.
	 */
	public static String getAppURL(HttpServletRequest request) {
		StringBuilder url = new StringBuilder();
		int port = request.getServerPort();
		if (port < 0) {
			port = 80; // Work around java.net.URL bug
		}
		String scheme = request.getScheme();
		url.append(scheme);
		url.append("://");
		url.append(request.getServerName());
		if ((scheme.equals("http") && (port != 80))
				|| (scheme.equals("https") && (port != 443))) {
			url.append(':');
			url.append(port);
		}
		url.append(request.getContextPath());
		return url.toString();
	}
}
